<template>
  <div>
    <el-dialog :title="textData" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form :rules="rules" ref="fileData" :model="fileData" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="fileData.title"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="fileData.desc" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="封面">
          <!-- 上传头像 -->
          <ImageUpload ref="upload"></ImageUpload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button type="primary" @click="addFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addCategories, getCategorieSpecify,editCategories } from "@/api/categories.js";
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    textData: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      fileData: {
        title: "",
        desc: "",
        image: "",
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        desc: [{ required: true, message: "请输入描述", trigger: "blur" }],
        image: [{ required: true, message: "请上传图片", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 编辑
    async pupListFn(id) {
      const res = await getCategorieSpecify(id)
      this.fileData = res.data
      this.$refs.upload.fileList.push({
        url: res.data.image
      })
    },
    // 添加
    async addFn() {
      if (this.fileData.id) {
        this.fileData.image = this.$refs.upload.fileList[0].url
        await editCategories(this.fileData)
      } else {
        this.$refs.fileData.validate();
        this.fileData.image = this.$refs.upload.fileList[0].url
        await addCategories(this.fileData);
        this.$emit("addTextFn");
        this.cancelFn();
        this.$refs.upload.fileList = []
      }
      this.$emit("addTextFn")
      this.cancelFn()
      this.$refs.upload.removeFn()
    },
    // 弹框状态
    handleClose() {
      this.$refs.fileData.resetFields();
      this.fileData = {
        title: "",
        desc: "",
        image: "",
      };
      this.$emit("cancel");
    },
    cancelFn() {
      this.$refs.fileData.resetFields();
      this.fileData = {
        title: "",
        desc: "",
        image: "",
      };
      this.$emit("cancel");
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>